package com.siwei.mes.service.laboratory.impl;

import com.alibaba.fastjson.JSON;
import com.siwei.mes.common.util.NullUtils;
import com.siwei.mes.entity.laboratory.ProduceHnt;
import com.siwei.mes.entity.sync.erp.ErpCustomer;
import com.siwei.mes.entity.sync.erp.ErpProject;
import com.siwei.mes.mapper.laboratory.ProduceHntMapper;
import com.siwei.mes.mapper.syncErp.ErpCustomerMapper;
import com.siwei.mes.service.laboratory.ProduceHntService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 协会混凝土任务实现
 *
 * @Description: 协会混凝土任务实现
 * @Version: 1.0
 * @author: mlchen
 * @date: 2024/9/14 21:28
 */
@Service
public class ProduceHntServiceImpl implements ProduceHntService {
    private static final Logger log = LoggerFactory.getLogger("laboratory");
    @Resource
    private ProduceHntMapper produceHntMapper;
    @Resource
    private ErpCustomerMapper erpCustomerMapper;

    @Override
    public void updateProduceHntByErpProject(ErpProject erpProject, boolean isNotify) {
        ProduceHnt produceHnt = new ProduceHnt();
        try {
            ErpCustomer erpCustomer = erpCustomerMapper.loadById(erpProject.getErpCustomerId());
            if (erpCustomer != null) {
                produceHnt.setProduceCustId(NullUtils.ifNull(erpCustomer.getLaboratoryId()));
            } else {
                produceHnt.setProduceCustId("");
            }
            produceHnt.setProduceContractId(erpProject.getLaboratoryId());

            produceHnt.setProduceConstructUnit(NullUtils.ifNull(erpProject.getContractName()));
            produceHnt.setProduceProjectName(NullUtils.ifNull(erpProject.getLaboratoryProjectName()));
            produceHnt.setProduceProjectAddress(NullUtils.ifNull(erpProject.getProjectAddress()));
            produceHnt.setProduceLinkMan(NullUtils.ifNull(erpProject.getLinkMan()));
            produceHnt.setProduceLinkPhone(NullUtils.ifNull(erpProject.getLinkTel()));
            produceHnt.setProduceContractId(NullUtils.ifNull(erpProject.getLaboratoryId()));
            produceHnt.setProduceContractBjbh(NullUtils.ifNull(erpProject.getBjbh(), "---"));
            produceHnt.setProduceContractJsdw(NullUtils.ifNull(erpProject.getJsdw()));
            produceHnt.setProduceContractJldw(NullUtils.ifNull(erpProject.getJldw()));
            log.info("修改协会混凝土任务单，协会客户id：【{}】协会合同id：【{}】，入参：【{}】", produceHnt.getProduceCustId(), produceHnt.getProduceContractId(), JSON.toJSONString(produceHnt));
            produceHntMapper.updateProduceHntByErpProjectToCustIdAndContractId(produceHnt);
        } catch (Exception e) {
            log.info("根据品控工程信息修改协会混凝土任务信息失败，入参：{}", JSON.toJSONString(produceHnt));
            log.info("根据品控工程信息修改协会混凝土任务信息失败，错误信息：{}", e.getMessage(), e);
            assertTrue(isNotify, "根据品控工程信息修改协会混凝土任务信息失败，失败原因：【" + e.getMessage() + "】");
        }
    }

    @Override
    public void updateProduceHntByErpProject(String produceId, ErpProject erpProject, boolean isNotify) {
        ProduceHnt produceHnt = new ProduceHnt();
        try {
            ErpCustomer erpCustomer = erpCustomerMapper.loadById(erpProject.getErpCustomerId());
            if (erpCustomer != null) {
                produceHnt.setProduceCustId(NullUtils.ifNull(erpCustomer.getLaboratoryId()));
            } else {
                produceHnt.setProduceCustId("");
            }
            produceHnt.setProduceId(produceId);
            produceHnt.setProduceConstructUnit(NullUtils.ifNull(erpProject.getContractName()));
            produceHnt.setProduceProjectName(NullUtils.ifNull(erpProject.getLaboratoryProjectName()));
            produceHnt.setProduceProjectAddress(NullUtils.ifNull(erpProject.getProjectAddress()));
            produceHnt.setProduceLinkMan(NullUtils.ifNull(erpProject.getLinkMan()));
            produceHnt.setProduceLinkPhone(NullUtils.ifNull(erpProject.getLinkTel()));
            produceHnt.setProduceContractId(NullUtils.ifNull(erpProject.getLaboratoryId()));
            produceHnt.setProduceContractBjbh(NullUtils.ifNull(erpProject.getBjbh(), "---"));
            produceHnt.setProduceContractJsdw(NullUtils.ifNull(erpProject.getJsdw()));
            produceHnt.setProduceContractJldw(NullUtils.ifNull(erpProject.getJldw()));
            log.info("修改协会混凝土任务单，协会混凝土任务单id：【{}】，入参：【{}】", produceId, JSON.toJSONString(produceHnt));
            produceHntMapper.updateProduceHntByErpProject(produceHnt);
        } catch (Exception e) {
            log.info("根据品控工程信息修改协会混凝土任务信息失败，入参：{}", JSON.toJSONString(produceHnt));
            log.info("根据品控工程信息修改协会混凝土任务信息失败，错误信息：{}", e.getMessage(), e);
            assertTrue(isNotify, "根据品控工程信息修改协会混凝土任务信息失败，失败原因：【" + e.getMessage() + "】");
        }
    }
}
